package com.wanpinghui.supplier.addrbook;

import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.business.InputActivity;
import com.wanpinghui.supplier.business.PersonalCircleActivity;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.message.MessageP2PActivity;
import com.wanpinghui.supplier.product.ProductListActivity;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.addrbook.AddrbookService;
import com.wanpinghui.wphlibrary.model.addrbook.bean.MoblieBean;
import com.wanpinghui.wphlibrary.model.business.BusinessService;
import com.wanpinghui.wphlibrary.model.business.bean.DynamicBean;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserInformation;
import com.wanpinghui.wphlibrary.photovideo.bean.BaseUrlBean;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.view.YunShlPopupWinow;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import retrofit.callback.YRequestCallback;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/20
 ***/
@ContentView(R.layout.layout_activity_customer_info2)
public class CustomerInfoActivity extends BaseActivity {

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser tpl_title;

    private MoblieBean bean;
    @ViewInject(R.id.iv_head)
    private ImageView ivHead;

    @ViewInject(R.id.tv_name)
    private TextView tvName;

    @ViewInject(R.id.tv_com)
    private TextView tv_com;

    @ViewInject(R.id.ll_dynamic)
    private LinearLayout ll_dynamic;

    @ViewInject(R.id.image_view1)
    private ImageView image_view1;

    @ViewInject(R.id.image_view2)
    private ImageView image_view2;

    @ViewInject(R.id.image_view3)
    private ImageView image_view3;

    @ViewInject(R.id.image_view4)
    private ImageView image_view4;

    @ViewInject(R.id.tv_address)
    private TextView tv_address;

    @ViewInject(R.id.tv_phone)
    private TextView tv_phone;

    @ViewInject(R.id.iv_authentication)
    private ImageView iv_authentication;

    private int user_id;

    private YunShlPopupWinow popupWinow;
    private int mineId;
    private String avatarUrl;
    private String userName;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        bean = (MoblieBean) getIntent().getSerializableExtra("bean");
        UserBean userBean = WPHSdk.getService(OAuthService.class).getUserInfo(null);
        user_id = getIntent().getIntExtra("user_id",0);
        if (bean != null && userBean != null) {
            user_id = (int) bean.getUser_id();
            mineId = Integer.parseInt(userBean.getUser().getId());
            userName = bean.getNickname();
            avatarUrl = bean.getAvatar();
            Glide.with(getApplicationContext()).load(bean.getAvatar()).placeholder(R.mipmap.my_icon_head_default).into(ivHead);
            tvName.setText(bean.getNickname());
            getUserInfo();
            searchLatelyDynamicList();
            //tvCompanyLevel.setText(bean.getCom()+"  "+bean.getLevelstr() +" 会员");
//            tvAddDetail.setText(bean.getProv()+","+bean.getCity());
//            tvCompanyName.setText(bean.getCom());
        }else if (user_id >0){
            getUserInfo();
            searchLatelyDynamicList();
        }
    }

    public static void start(Activity context, long user_id) {
        Intent intent = new Intent();
        intent.putExtra("user_id", user_id);
        intent.setClass(context, CustomerInfoActivity.class);
       context.startActivity(intent);
    }

    private void searchLatelyDynamicList() {
        WPHSdk.getService(BusinessService.class)
                .searchLatelyDynamicList(user_id, new YRequestCallback<List<DynamicBean>>() {
                    @Override
                    public void onSuccess(List<DynamicBean> var1) {
                        List<String> baseUrlBeanList = new ArrayList<>();
                        for (DynamicBean bean : var1){
                            if (TextUtil.isNotEmpty(bean.getPictures())){
                                List<BaseUrlBean> baseUrlBeanList1 = new Gson().fromJson(bean.getPictures(),new TypeToken<List<BaseUrlBean>>(){}.getType());
                                if (baseUrlBeanList1 != null && baseUrlBeanList1.size()>0){
                                    for (BaseUrlBean bean1 : baseUrlBeanList1 ){
                                        baseUrlBeanList.add(bean1.getThumbUrl());
                                    }
                                }
                            }
                        }
                        if (baseUrlBeanList != null && baseUrlBeanList.size()>0){
                            switch (baseUrlBeanList.size()){
                                case 1:
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(0)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view1);
                                    break;
                                case 2:
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(0)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view1);
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(1)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view2);
                                    break;
                                case 3:
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(0)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view1);
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(1)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view2);
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(2)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view3);
                                    break;
                                default:
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(0)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view1);
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(1)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view2);
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(2)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view3);
                                    Glide.with(getApplicationContext()).load(baseUrlBeanList.get(3)).placeholder(R.mipmap.goods_bg_loading_a).into(image_view4);
                                    break;
                            }
                        }

                    }

                    @Override
                    public void onFailed(int var1, String message) {

                    }

                    @Override
                    public void onException(Throwable var1) {

                    }
                });
    }

    @Override
    public void bindEvents() {
        tpl_title.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.ll_send_msg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageP2PActivity.start(CustomerInfoActivity.this, String.valueOf(user_id));
            }
        });
        findViewById(R.id.ll_trade).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //showTradeView();
                Intent intent = new Intent(CustomerInfoActivity.this, ProductListActivity.class);
                intent.putExtra("buyer", mineId);
                intent.putExtra("supplier",user_id);
                startActivity(intent);
            }
        });
        ll_dynamic.setOnClickListener(new View.OnClickListener() {  //个人动态
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CustomerInfoActivity.this, PersonalCircleActivity.class);
                intent.putExtra("id",user_id);
                intent.putExtra("name", userName);
                intent.putExtra("avatar",avatarUrl);
                startActivity(intent);
            }
        });

    }


    private void showTradeView() {
        View view = LayoutInflater.from(CustomerInfoActivity.this).inflate(R.layout.view_trade, null);
        if (popupWinow == null)
            popupWinow = new YunShlPopupWinow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWinow.setCommonConfig(CustomerInfoActivity.this, YunShlPopupWinow.AnimaDirect.DIRECT_BOTTOM_TOP);
        TextView tv_sale = (TextView) view.findViewById(R.id.tv_sale);
        TextView tv_buy = (TextView) view.findViewById(R.id.tv_buy);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_sale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
                Intent intent = new Intent(CustomerInfoActivity.this, ProductListActivity.class);
                intent.putExtra("buyer", user_id);
                intent.putExtra("supplier",mineId);
                startActivity(intent);
            }
        });
        tv_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
                Intent intent = new Intent(CustomerInfoActivity.this, ProductListActivity.class);
                intent.putExtra("buyer", mineId);
                intent.putExtra("supplier",user_id);
                startActivity(intent);
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
            }
        });

        popupWinow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void initData() {



    }

    private void getUserInfo() {
        WPHSdk.getService(AddrbookService.class).getUserInfo(user_id, new YRequestCallback<UserBean>() {
            @Override
            public void onSuccess(UserBean var1) {
                if (var1 != null && var1.getUser() != null) {
                    UserInformation userInformation = var1.getUser();
                    tvName.setText(userInformation.getNickname());
                    tv_phone.setText(userInformation.getMobile());
                    user_id = userInformation.getIntId();
                    avatarUrl = userInformation.getAvatar();
                    userName = userInformation.getNickname();
                    if (TextUtil.isNotEmpty(userInformation.getCom())) {
                        if (TextUtil.isNotEmpty(userInformation.getPos())) {
                            tv_com.setText(userInformation.getCom() + " | " + userInformation.getPos());
                        } else {
                            tv_com.setText(userInformation.getCom() + " | 暂无");
                        }
                    } else {
                        tv_com.setText("暂无他的公司信息/职称");
                    }
                    if (userInformation.isPass()) {
                        iv_authentication.setVisibility(View.VISIBLE);
                    } else {
                        iv_authentication.setVisibility(View.GONE);
                    }
                    if (TextUtil.isNotEmpty(userInformation.getAddr())){
                        tv_address.setText(userInformation.getAddr());
                    }else {
                        tv_address.setText(userInformation.getPhone_area_name());
                    }
                    Glide.with(getApplicationContext()).load(userInformation.getAvatar()).placeholder(R.mipmap.address_list_bg_head).bitmapTransform(new CropCircleTransformation(CustomerInfoActivity.this)).into(ivHead);
                }
            }

            @Override
            public void onFailed(int var1, String message) {
                showWarningText("获取信息失败");

            }

            @Override
            public void onException(Throwable var1) {
                showWarningText("获取信息失败");
            }
        });
    }


}
